package com.xb.p8_contextmenu;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class ContextMenu2Activity extends AppCompatActivity {
    //1.建立动作模式的对象
    private ActionMode actionMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_context_menu2);

        /*Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });*/

        //4.创建监控动作栏生命周期的回调函数
        final ActionMode.Callback callback=new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                //事件触发时，填充设计好的菜单界面
                mode.getMenuInflater().inflate(R.menu.menu_main,menu);
                //默认的返回值为false，一定要改为true
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                //每一个菜单项事件处理
                switch(item.getItemId()){
                    case R.id.action_setting:
                        Toast.makeText(getApplicationContext(),"点击了yes菜单选项",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.action_file:
                        Toast.makeText(getApplicationContext(),"点击了NO菜单选项",Toast.LENGTH_LONG).show();
                        break;
                    default:
                        return false;
                }
                return true;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }
        };

        //2.获取用户要操作的imageview,监听用户的动作
        ImageView imageview2=findViewById(R.id.image_ting2);
        imageview2.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                //Toast.makeText(getApplicationContext(),"长按事件",Toast.LENGTH_LONG).show();
                if(actionMode !=null){

                    //解决第二次进来的时候，actionmode不为空，不能够再次显示。
                    //添加将actionmode置为null，不要返回false。
                    actionMode=null;
                    //return false;

                }
                //3.在回调函数中处理动作栏的相关动作
                actionMode=startActionMode(callback);
                //必须设置每个菜单项是可以选中的
                view.setSelected(true);
                return true;
            }
        });

    }



}
